package com.demo.aop.core;

import lombok.Data;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: NOVOTS
 * @date: 2019-04-29 17:56
 */
@Data
public class FyProxy {
    private Object proxy;
    private Object object;
    private List<FyIntercepterAdapter> list;

    public FyProxy() {
        list = new ArrayList<FyIntercepterAdapter>();
    }
    /**
    * @redeme: 执行前置拦截器的方法，通过参数决定是否该执行该拦截器的前置拦截
    * @author  tjh
    * @date 2019/4/29 17:59
    */
    public boolean doBefore(Method method,Object... objs){
        for (FyIntercepterAdapter item : list) {
            if(!item.getMethod().equals(method)){
                continue;
            }
            if(!item.before(objs)){
                return false;
            }
        }
        return true;
    }
    /**
    * @redeme:  后置拦截
    * @author  tjh
    * @date 2019/4/29 18:07
    */
    public Object doAfter(Method method,Object result) {
        for (FyIntercepterAdapter item : list) {
            if (!item.getMethod().equals(method)) {
                continue;
            }
            return item.after(result);
        }
        return result;
    }

    public void doDealException(Method method,Throwable throwable) throws Throwable{
        for (FyIntercepterAdapter item : list) {
            if (!item.getMethod().equals(method) ) {
                continue;
            }
            item.dealException(throwable);
            throw throwable;
        }


    }

}
